# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: zimbatm <zimbatm@zimbatm.com>
# Contributor: ShadowKyogre <shadowkyogre.public+aur@gmail.com>
# Contributor: rmorgans <rick.morgans@gmail.com>

pkgname=direnv
pkgver=2.32.2
pkgrel=1
pkgdesc='A shell extension that manages your environment'
arch=('loong64' 'x86_64')
url='https://direnv.net'
license=('MIT')
depends=('glibc')
makedepends=('git' 'go')
checkdepends=('zsh' 'fish' 'bash' 'tcsh' 'elvish')
options=('!lto')
_commit='357825f4ddcc8d7ef62a6276b60d9aa36449ac96'
source=("$pkgname::git+https://github.com/direnv/direnv.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # download dependencies
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"

  go build -v \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode external -extldflags ${LDFLAGS}" \
    .
}

check() {
  cd "$pkgname"

  # run go tests
  go test -v ./...

  # run shell-specific tests
  bash ./test/direnv-test.bash
  #elvish ./test/direnv-test.elv # temporarily skip failing test :<
  fish ./test/direnv-test.fish
  tcsh -e ./test/direnv-test.tcsh
  zsh ./test/direnv-test.zsh

  # run stdlib tests
  ./test/stdlib.bash
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" "$pkgname"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/man/man1" man/*.1
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname/docs" docs/*

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}
